package com.cjjd.c.feature.mine.order;

import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cjjd.c.R;
import com.cjjd.c.ShareKeys;
import com.cjjd.c.base.MyBaseAdapter;
import com.cjjd.c.bean.CheckInItemBean;
import com.cjjd.c.bean.CheckoutItemBean;
import com.cjjd.c.bean.HttpResult;
import com.cjjd.c.bean.RoomOrderItembean;
import com.cjjd.c.feature.dialog.CheckoutDialog;
import com.cjjd.c.feature.dialog.CheckoutRoomCardDialog;
import com.cjjd.c.feature.dialog.TextDialog;
import com.cjjd.c.feature.home.checkout.CheckOutInfoActivity;
import com.cjjd.c.feature.home.checkout.CheckoutWithoutReceiptActivity;
import com.cjjd.c.feature.home.checkout.CountDownActivity;
import com.cjjd.c.feature.home.checkout.ReceiptDealActivity;
import com.cjjd.c.feature.home.chekin.checklist_detail.CheckinDetailActivity;
import com.cjjd.c.http.CallbackImpl;
import com.cjjd.c.http.JsonUtils;
import com.cjjd.c.http.RetrofitSingleton;
import com.cjjd.c.utils.LogUtil;
import com.cjjd.c.utils.SharePrefUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Response;


/**
 * ----------------------------------
 * Created by xyd on 2017/8/15.
 * <p/>
 * 描  述：
 * <p/>
 * 修订历史:
 * ----------------------------------
 */

public class OrderAdapter extends MyBaseAdapter {

    public static final int CHECK_IN = 0x00000001;          // 入住
    public static final int CHECK_OUT = 0x00000002;         // 退房
    public static final int ORDER = 0x00000003;             // 订单列表
    public static final int ACCOUNTCHECK = 0x00000004;      // 在线结账
    private int type = 0;

    public OrderAdapter(FragmentActivity mContext, int intoType) {
        super(mContext);
        this.type = intoType;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (viewHolder == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_order, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        // 办理入住
        //<editor-fold desc="办理入住">
        if (type == CHECK_IN) {

            final CheckInItemBean itembean = (CheckInItemBean) getItem(position);

            viewHolder.tvHotelName.setText(itembean.hotelName);
            viewHolder.tvOrderNum.setText("订单号: " + itembean.orderId);
            viewHolder.tvPrice.setText("￥" + itembean.orderMoney);
            viewHolder.tvHotelStatus.setText(itembean.stateName);
            viewHolder.tvHotelStyle.setText(itembean.roomTypeName + " " + itembean.breakfastTypeName
                    + " " + itembean.checkInDayNum + "晚/" + itembean.roomNums + "间");

            viewHolder.btnNext.setText(itembean.stateName);
            viewHolder.btnNext.setText("办理入住");
            viewHolder.btnNext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SharePrefUtil.saveString(mContext, ShareKeys.hotelUuid, itembean.hotelId);
                    Intent intent = new Intent(mContext, CheckinDetailActivity.class);
                    intent.putExtra("mainUuid", itembean.orderMainUuid);
                    intent.putExtra("detailUuid", itembean.orderDetailUuid);
                    mContext.startActivity(intent);
                }
            });
        }
        //</editor-fold>
        // 办理退房
        //<editor-fold desc="办理退房">
        if (type == CHECK_OUT) {

            final CheckoutItemBean itemBean = (CheckoutItemBean) getItem(position);

            viewHolder.tvHotelName.setText(itemBean.hotelName);
            if (Integer.parseInt(itemBean.state) == 5) {
                viewHolder.tvPrice.setText("￥" + itemBean.orderMoney);
            } else {
                viewHolder.tvPrice.setText("￥----");
            }
            viewHolder.tvOrderNum.setText("订单号：" + itemBean.orderId);
            // 3：待退房；4：带查房；5：待结账
            viewHolder.tvHotelStatus.setText(itemBean.stateName);
            if (Integer.parseInt(itemBean.state) == 3) {
                viewHolder.btnNext.setText("立即退房");
            } else if (Integer.parseInt(itemBean.state) == 4) {
                viewHolder.btnNext.setText("查看进度");
            } else if (Integer.parseInt(itemBean.state) == 5) {
                viewHolder.btnNext.setText("立即结账");
            }

            StringBuffer showRoom = new StringBuffer();
                for (String s : itemBean.roomNos) {
                    if(itemBean.roomNos.size()>1) {
                        showRoom.append("、").append(s);
                    }else {
                        showRoom.append(s);
                    }

            }

            viewHolder.tvHotelStyle.setText("房号：" + showRoom.toString());

            viewHolder.btnNext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Integer.parseInt(itemBean.state) == 3) {
                        if ("1".equals(itemBean.isFront)) {
                            new TextDialog(mContext, "请到前台办理退房", new TextDialog.OnCloseListener() {
                                @Override
                                public void OnClick() {

                                }
                            }).show();
                        } else {
                            new CheckoutDialog(mContext, new CheckoutDialog.OnClickListener() {
                                @Override
                                public void onPositiveClick() {
                                    // 需要发票

                                    new CheckoutRoomCardDialog(mContext, new CheckoutRoomCardDialog.OnClickListener() {
                                        @Override
                                        public void onPositiveClick() {
                                            Intent intent = new Intent(mContext, ReceiptDealActivity.class);
                                            intent.putExtra("orderMainUuid", itemBean.orderMainUuid);
                                            intent.putExtra("type","2");
                                            mContext.startActivity(intent);
                                        }

                                        @Override
                                        public void onNegativeClick() {
                                            Intent intent = new Intent(mContext, ReceiptDealActivity.class);
                                            intent.putExtra("orderMainUuid", itemBean.orderMainUuid);
                                            intent.putExtra("type","1");
                                            mContext.startActivity(intent);
                                        }
                                    }).show();
                                }

                                @Override
                                public void onNegativeClick() {
                                    // 不需要发票
                                    // 调用退房，用来更改状态  chekOutOrder
                                    //TODO 不需要发票需要通知服务器
                                    new CheckoutRoomCardDialog(mContext, new CheckoutRoomCardDialog.OnClickListener() {
                                        @Override
                                        public void onPositiveClick() {
                                            chekOutOrder(itemBean.orderMainUuid, itemBean.hotelMobile,"2");

                                        }

                                        @Override
                                        public void onNegativeClick() {
                                            chekOutOrder(itemBean.orderMainUuid, itemBean.hotelMobile,"1");

                                        }
                                    }).show();
                                }
                            }).show();
                        }


                    } else if (Integer.parseInt(itemBean.state) == 4) {
                        //viewHolder.btnNext.setText("查看进度");
//                        Intent intent = new Intent(mContext, CountDownActivity.class);
//                        intent.putExtra("orderMainUuid", itemBean.orderMainUuid);
//                        mContext.startActivity(intent);
                        if ("0".equals(itemBean.needInvoice)) {
                            //TODO dada
                            //不需要发票
                            Intent intent = new Intent(mContext, CheckoutWithoutReceiptActivity.class);
                            // TODO: 2017/10/13 这里需要酒店uuid
                            intent.putExtra("orderMainUuid", itemBean.orderMainUuid);
                            intent.putExtra("dianhua", itemBean.hotelMobile);

                            mContext.startActivity(intent);
                        } else {
                            Intent intent = new Intent(mContext, CountDownActivity.class);
                            intent.putExtra("orderMainUuid", itemBean.orderMainUuid);
                            intent.putExtra("dianhua", itemBean.hotelMobile);


                            mContext.startActivity(intent);
                        }

                    } else if (Integer.parseInt(itemBean.state) == 5) {
                        Intent intent = new Intent(mContext, CheckOutInfoActivity.class);
                        intent.putExtra("orderMainUuid", itemBean.orderMainUuid);
                        mContext.startActivity(intent);
                    }
                }


            });
        }
        //</editor-fold>

        // 订单列表 ORDER
        //<editor-fold desc="订单列表">
        if (type == ORDER) {

            final RoomOrderItembean itemBean = (RoomOrderItembean) getItem(position);

            viewHolder.tvHotelName.setText(itemBean.hotelName);
            viewHolder.tvOrderNum.setText("订单号:" + itemBean.orderId);
            viewHolder.tvPrice.setText("￥" + itemBean.orderMoney);
            viewHolder.tvHotelStatus.setText(itemBean.stateName);
            viewHolder.tvHotelStyle.setText(itemBean.roomTypeName + " " + itemBean.breakfastTypeName
                    + " " + itemBean.checkInDayNum + "晚/" + itemBean.roomNums + "间");

            //    状态名称 1.待支付（支付操作）
            //            2.待入住（选房操作）
            //            3.待退房（退房操作）
            //            4.待查房（查房操作）
            //            5.待结账（结账操作）
            //            6.已完成
            //            7.已取消
            if (Integer.parseInt(itemBean.state) == 1) {
                viewHolder.btnNext.setText("立即支付");
            } else if (Integer.parseInt(itemBean.state) == 2) {
                viewHolder.btnNext.setText("立即选房");
            } else if (Integer.parseInt(itemBean.state) == 3) {
                viewHolder.btnNext.setText("立即退房");
            } else if (Integer.parseInt(itemBean.state) == 4) {
                viewHolder.btnNext.setText("查看进度");
            } else if (Integer.parseInt(itemBean.state) == 5) {
                viewHolder.btnNext.setText("立即结账");
            } else if (Integer.parseInt(itemBean.state) == 6) {
                viewHolder.btnNext.setText("查看详情");
            } else if (Integer.parseInt(itemBean.state) == 7) {
                viewHolder.btnNext.setText("已取消");
                viewHolder.btnNext.setEnabled(false);
            }

            viewHolder.btnNext.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (Integer.parseInt(itemBean.state) == 1 || Integer.parseInt(itemBean.state) == 2) {
                        SharePrefUtil.saveString(mContext, ShareKeys.hotelUuid, itemBean.hotelId);
                        Intent intent = new Intent(mContext, CheckinDetailActivity.class);
                        intent.putExtra("mainUuid", itemBean.orderMainUuid);
                        intent.putExtra("detailUuid", itemBean.orderDetailUuid);
                        mContext.startActivity(intent);
                    } else if (Integer.parseInt(itemBean.state) == 3) {
                        if ("1".equals(itemBean.isFront)) {
                            new TextDialog(mContext, "请到前台办理退房", new TextDialog.OnCloseListener() {
                                @Override
                                public void OnClick() {

                                }
                            }).show();
                        } else {
                            new CheckoutDialog(mContext, new CheckoutDialog.OnClickListener() {
                                @Override
                                public void onPositiveClick() {
                                    // 需要发票

                                    new CheckoutRoomCardDialog(mContext, new CheckoutRoomCardDialog.OnClickListener() {
                                        @Override
                                        public void onPositiveClick() {
                                            Intent intent = new Intent(mContext, ReceiptDealActivity.class);
                                            intent.putExtra("orderMainUuid", itemBean.orderMainUuid);
                                            intent.putExtra("type","2");
                                            mContext.startActivity(intent);
                                        }

                                        @Override
                                        public void onNegativeClick() {
                                            Intent intent = new Intent(mContext, ReceiptDealActivity.class);
                                            intent.putExtra("orderMainUuid", itemBean.orderMainUuid);
                                            intent.putExtra("type","1");
                                            mContext.startActivity(intent);
                                        }
                                    }).show();

                                }

                                @Override
                                public void onNegativeClick() {
                                    // 不需要发票
                                    // 调用退房，用来更改状态  chekOutOrder
                                    // 不需要发票需要通知服务器
                                    new CheckoutRoomCardDialog(mContext, new CheckoutRoomCardDialog.OnClickListener() {
                                        @Override
                                        public void onPositiveClick() {
                                            chekOutOrder(itemBean.orderMainUuid, itemBean.hotelMobile,"2");

                                        }

                                        @Override
                                        public void onNegativeClick() {
                                            chekOutOrder(itemBean.orderMainUuid, itemBean.hotelMobile,"1");

                                        }
                                    }).show();
                                }
                            }).show();
                        }


                    } else if (Integer.parseInt(itemBean.state) == 4) {
                        //viewHolder.btnNext.setText("查看进度");

                        if ("0".equals(itemBean.needInvoice)) {
                            //TODO dada
                            //不需要发票
                            Intent intent = new Intent(mContext, CheckoutWithoutReceiptActivity.class);
                            // TODO: 2017/10/13 这里需要酒店uuid
                            intent.putExtra("dianhua", itemBean.hotelMobile);
                            intent.putExtra("orderMainUuid", itemBean.orderMainUuid);
                            mContext.startActivity(intent);
                        } else {
                            Intent intent = new Intent(mContext, CountDownActivity.class);
                            intent.putExtra("orderMainUuid", itemBean.orderMainUuid);
                            intent.putExtra("dianhua", itemBean.hotelMobile);

                            mContext.startActivity(intent);
                        }


                    } else if (Integer.parseInt(itemBean.state) == 5) {
                        Intent intent = new Intent(mContext, CheckOutInfoActivity.class);
                        intent.putExtra("orderMainUuid", itemBean.orderMainUuid);
                        intent.putExtra("dianhua", itemBean.hotelMobile);

                        mContext.startActivity(intent);
                    } else if (Integer.parseInt(itemBean.state) == 6) {
                        Intent intent = new Intent(mContext, OrderDetailActivity.class);
                        intent.putExtra("orderMainUuid", itemBean.orderMainUuid);
                        intent.putExtra("detailUuid", itemBean.orderDetailUuid);
                        intent.putExtra("dianhua", itemBean.hotelMobile);

                        mContext.startActivity(intent);
                    } else if (Integer.parseInt(itemBean.state) == 7) {

                    }
                }
            });

        }
        //</editor-fold>


        return convertView;
    }

    /**
     * @param orderUuid
     */
    private void chekOutOrder(final String orderUuid, final String dianhua,final String returnCardType) {

        LogUtil.e("xyd", "orderMainUuid=" + orderUuid);
        HashMap<String, Object> h1 = new HashMap<>();
        h1.put("token", SharePrefUtil.getString(mContext, ShareKeys.Token, ""));
        h1.put("sessionId", SharePrefUtil.getString(mContext, ShareKeys.sessionId, ""));
        h1.put("orderUuid", orderUuid);
        h1.put("needInvoice", "0");   // 是否需要发票（1：是；0：否）
        h1.put("returnCardType",returnCardType);//房卡退还形式（ 1：放至前台；2：放至房间）

        Call<HttpResult<String>> call = RetrofitSingleton.getService(mContext).chekOutOrder(JsonUtils.convertParam("chekOutOrder", h1));
        call.enqueue(new CallbackImpl<String>() {
            @Override
            public void responseSuccess(Call<HttpResult<String>> call, Response<HttpResult<String>> response) {
                Intent intent = new Intent(mContext, CheckoutWithoutReceiptActivity.class);
                // TODO: 2017/10/13 这里需要酒店uuid
                intent.putExtra("orderMainUuid", orderUuid);
                intent.putExtra("dianhua", dianhua);
                mContext.startActivity(intent);
            }
        });
    }


    public void setFrom(int from) {
        this.type = from;
    }

    static class ViewHolder {
        @Bind(R.id.tv_hotel_name)
        TextView tvHotelName;
        @Bind(R.id.tv_order_num)
        TextView tvOrderNum;
        @Bind(R.id.tv_price)
        TextView tvPrice;
        @Bind(R.id.tv_hotel_status)
        TextView tvHotelStatus;
        @Bind(R.id.tv_hotel_style)
        TextView tvHotelStyle;
        @Bind(R.id.btn_next)
        TextView btnNext;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
